package com.ac.scum.log.task.face.log.counsumer.stages;

import com.ac.scum.log.task.face.log.counsumer.LogConsumer;
import com.ac.scum.log.task.face.log.counsumer.stages.gamplay.*;
import com.ac.scum.log.task.utils.queue.FTPQueueItem;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@AllArgsConstructor
public class GameplayLogConsumer implements LogConsumer {
    private final LockConsumer lockConsumer;
    private final TrapConsumer trapConsumer;
    private final FlagConsumer flagConsumer;
    private final ChestConsumer chestConsumer;
    private final BunkerConsumer bunkerConsumer;
    private final ExplosivesConsumer explosivesConsumer;
    private final BombDefusalConsumer bombDefusalConsumer;
    private final BunkerActiveConsumer bunkerActiveConsumer;


    public void consumerLog(FTPQueueItem ftpQueueItem) {
        try {
            writeAllPlayerInfoInCache(ftpQueueItem);
            List<GameLogLine> traps = Lists.newArrayList();
            List<GameLogLine> locks = Lists.newArrayList();
            List<GameLogLine> explosives = Lists.newArrayList();
            List<GameLogLine> bombDefusal = Lists.newArrayList();
            List<GameLogLine> flags = Lists.newArrayList();
            List<GameLogLine> logBunkerActiveList = Lists.newArrayList();
            List<GameLogLine> logBunkerList = Lists.newArrayList();
            List<GameLogLine> logChestList = Lists.newArrayList();
            for (GameLogLine line : splitLine(ftpQueueItem)) {
                String lineContext = line.getLineContext();
                if (lineContext.contains("[LogTrap]")) {
                    traps.add(line);
                    continue;
                }
                if (lineContext.contains("[LockpickingMinigame_C]") || lineContext.contains("[BP_LockBombDefusalMinigame_C]") || lineContext.contains("[BP_DialLockMinigame_C]")) {
                    locks.add(line);
                    continue;
                }
                if (lineContext.contains("[LogExplosives]") || lineContext.contains("[LogBaseBuilding] Item attached.")) {
                    explosives.add(line);
                    continue;
                }
                if (lineContext.contains("[BP_BombDefusalMinigame_C]")) {
                    bombDefusal.add(line);
                    continue;
                }
                if (lineContext.contains("[LogBaseBuilding]")) {
                    flags.add(line);
                    continue;
                }
                if (lineContext.contains("[LogBunkerLock]")) {
                    logBunkerActiveList.add(line);
                    continue;
                }
                if (lineContext.contains("[AbandonedBunkerVoltageMatchingMinigame_C]") || lineContext.contains("[AbandonedBunkerMasterSwitchboardMinigame_01_C]") || lineContext.contains("[AbandonedBunkerSlaveSwitchboardMinigame_01_C]") || lineContext.contains("[DialPadMinigame]") || lineContext.contains("[AbandonedBunkerDialPadMinigame_C]")) {
                    logBunkerList.add(line);
                    continue;
                }
                if (lineContext.contains("[LogChest]")) {
                    logChestList.add(line);
                    continue;
                }
                commitDiscardLog(ftpQueueItem, line, "gameplay no match");
            }
            trapConsumer.trapConsumer(traps, ftpQueueItem, this);
            lockConsumer.lockConsumer(locks, ftpQueueItem, this);
            flagConsumer.flagConsumer(flags, ftpQueueItem, this);
            chestConsumer.chestConsumer(logChestList, ftpQueueItem, this);
            bunkerConsumer.bunkerConsumer(logBunkerList, ftpQueueItem, this);
            explosivesConsumer.explosivesConsumer(explosives, ftpQueueItem, this);
            bombDefusalConsumer.bombDefusalConsumer(bombDefusal, ftpQueueItem, this);
            bunkerActiveConsumer.bunkerActiveConsumer(logBunkerActiveList, ftpQueueItem, this);
        } catch (Exception e) {
            log.error("gameplay 任务发生异常", e);
        }
    }
}
